//Licensed under GNU General Public License, version 3.0
var eMS =
{
	getSelectedText: function ()
	{
		var selection = document.commandDispatcher.focusedWindow.getSelection();

		if (selection)
		{
			var text = eMS.trim(selection.toString());
		}
		else
		{
			var text = "";
		}

		return text;
	},


	trim: function (text)
	{
		var myRegExPattern = /([\u0000-\u002F\u0040\u005B-\u0060\u007B-\u007F]|\b[Aa]nd\b|\b[Tt]he\b)+/g;
		var text = text.replace(myRegExPattern, ' ');
		text = text.replace(/(^\s)|(\s$)/g,'');

		return text;
	},


	getUrl: function (item, from)
	{
		var text = encodeURIComponent(eMS.lastSelection);

		switch (item)
		{
			case "customItem1":
			{
				return eMS.p.customItem1.url.replace(/%s/g, text);
			}
			default:
			{
				return item;
			}
		}
	},




	openItem: function (item, from)
	{
		var url = eMS.getUrl(item, from);

		if (!url)
		{
			return;
		}
		content.document.location.href=url;
	},



	init: function ()
	{

		eMS.ss = Components.classes["@mozilla.org/browser/sessionstore;1"]
							.getService(Components.interfaces.nsISessionStore);

		eMS.pf = Components.classes["@mozilla.org/preferences-service;1"]
							.getService(Components.interfaces.nsIPrefService)

		eMS.prefseMS = eMS.pf.getBranch("extensions.eMS.");
		eMS.prefsGeneral = eMS.pf.getBranch("general.");

		eMS.getPrefs();

		eMS.prefseMS.QueryInterface(Components.interfaces.nsIPrefBranch2);
		eMS.prefseMS.addObserver("", this, false);

		if (eMS.p.autoConfig)
		{
			eMS.autoConfig();
		}

		eMS.getMenuItems();

		function onPopupShowing (e)
		{
			if (e.target.id != "contentAreaContextMenu")
			{
				return;
			}

			eMS.drawMenu();
		}
		document.addEventListener("popupshowing", function (e) {onPopupShowing(e);}, false);
	},


	autoConfig: function ()
	{
		var locale = eMS.p.locale;
		eMS.prefseMS.setBoolPref("autoConfig", false);
	},


	getMenuItems: function ()
	{
		eMS.item_customItem1 = document.getElementById("eMS-customItem1");
		eMS.sep_customItem1 = document.getElementById("eMS-sep_customItem1");
		eMS.sep_searchItems = document.getElementById("eMS-sep-searchItems");
		eMS.stringsBundle = document.getElementById("eMS-stringsBundle");

		eMS.item_openlink = document.getElementById("context-openlink");
		eMS.item_bookmarklink = document.getElementById("context-bookmarklink");
		eMS.item_sendlink = document.getElementById("context-sendlink");
		eMS.item_sendimage = document.getElementById("context-sendimage");
		eMS.item_setDesktopBackground = document.getElementById("context-setDesktopBackground");
		eMS.item_blockimage = document.getElementById("context-blockimage");
		eMS.item_sendvideo = document.getElementById("context-sendvideo");
		eMS.item_sendaudio = document.getElementById("context-sendaudio");
		eMS.item_back = document.getElementById("context-back");
		eMS.item_forward = document.getElementById("context-forward");
		eMS.item_reload = document.getElementById("context-reload");
		eMS.item_stop = document.getElementById("context-stop");
		eMS.sep_stop = document.getElementById("context-sep-stop");
		eMS.item_bookmarkpage = document.getElementById("context-bookmarkpage");
		eMS.item_savepage = document.getElementById("context-savepage");
		eMS.item_sendpage = document.getElementById("context-sendpage");
		eMS.sep_viewbgimage = document.getElementById("context-sep-viewbgimage");
		eMS.item_viewbgimage = document.getElementById("context-viewbgimage");
		eMS.item_undo = document.getElementById("context-undo");
		eMS.sep_undo = document.getElementById("context-sep-undo");
		eMS.item_cut = document.getElementById("context-cut");
		eMS.item_copy = document.getElementById("context-copy");
		eMS.item_paste = document.getElementById("context-paste");
		eMS.item_delete = document.getElementById("context-delete");
		eMS.sep_paste = document.getElementById("context-sep-paste");
		eMS.item_selectall = document.getElementById("context-selectall");
		eMS.sep_selectall = document.getElementById("context-sep-selectall");
		eMS.item_keywordfield = document.getElementById("context-keywordfield");
		eMS.item_searchselect = document.getElementById("context-searchselect");
		eMS.sep_frame = document.getElementById("frame-sep");
		eMS.menu_frame = document.getElementById("frame");
		eMS.item_viewpartialsourceSelection = document.getElementById("context-viewpartialsource-selection");
		eMS.item_viewpartialsourceMathml = document.getElementById("context-viewpartialsource-mathml");
		eMS.sep_viewsource = document.getElementById("context-sep-viewsource");
		eMS.item_viewsource = document.getElementById("context-viewsource");
		eMS.item_viewinfo = document.getElementById("context-viewinfo");
		eMS.sep_spell = document.getElementById("spell-separator");
		eMS.item_spellCheck = document.getElementById("spell-check-enabled");

		eMS.item_saveimage = document.getElementById("context-saveimage");

		//To-Remove after 3.6
		eMS.sep_properties = document.getElementById("context-sep-properties");
		eMS.item_metadata = document.getElementById("context-metadata");
	},


	drawMenu: function ()
	{
		var text = eMS.getSelectedText();
		var doc = content.document;

		eMS.item_customItem1.hidden = true;
		eMS.sep_searchItems.hidden = true;

		//To-Remove
		if (text && !gContextMenu.onLink && (!gContextMenu.isOnImage && eMS.item_saveimage.hidden))
		{
			eMS.lastSelection = text;

			if (text.length > 20)
			{
				text = text.substr(0, 20) + "…";
			}

			if (eMS.p.customItem1.bool)
			{
				var label_customItem1 = eMS.stringsBundle.getFormattedString("customItemX.label", [eMS.p.customItem1.name, text]);

				eMS.item_customItem1.hidden = false;
				eMS.item_customItem1.label = label_customItem1;
				eMS.item_customItem1.setAttribute("accesskey", "M");
			}
		}
	},


	p:
	{
		customItem1: {},
	},



	getPrefs: function ()
	{
		eMS.p.locale = eMS.prefsGeneral.getCharPref("useragent.locale");
		eMS.p.autoConfig = eMS.prefseMS.getBoolPref("autoConfig");
		eMS.p.customItem1.bool = eMS.prefseMS.getBoolPref("customItem1.bool");
		eMS.p.customItem1.name = eMS.prefseMS.getCharPref("customItem1.name");
		eMS.p.customItem1.url = eMS.prefseMS.getCharPref("customItem1.url");
	},


}

window.addEventListener("load", function () {eMS.init();}, false);